package com.cxs.xiaomi.web.controller;

import com.cxs.xiaomi.utils.StringUtil;

import javax.servlet.ServletOutputStream;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.net.URLDecoder;
import java.util.List;

/*
 * @Author:cxs
 * @Date:2021/9/14  16:13
 * @Version 1.0
 * @Motto:放下杂念,只为迎接明天更好的自己
 * 图片请求地址
 * */
@WebServlet(name = "ImageServlet", urlPatterns = "/goodsImgServlet")
public class ImageServlet extends BaseServlet {

    public void lodding(HttpServletRequest request, HttpServletResponse response) {
        String basePath = request.getServletContext().getRealPath("WEB-INF" + File.separator + "images");
        String imgPath = request.getParameter("p");
        if (!StringUtil.empty(imgPath)) {
            try {
                String imagePath = URLDecoder.decode(imgPath, "utf-8");
                String imgRealPath = basePath + File.separator + imagePath;
                FileInputStream fin = new FileInputStream(imgRealPath);
                ServletOutputStream oos = response.getOutputStream();
                byte[] bytes = new byte[1024 * 4];
                int len;
                while ((len = fin.read(bytes)) != -1) {
                    oos.write(bytes, 0, len);
                    oos.flush();
                }
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    // 测试
    public void test(HttpServletRequest request, HttpServletResponse response) {
        System.out.println("测试方法执行");
    }
}
